import 'package:du_application/provide/IndexProvider.dart';
import 'package:du_application/routes/application.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:provide/provide.dart';
import './indexPage.dart';
import '../conpoments/bottomBar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class HomePage extends StatefulWidget {
   String keyword;
  HomePage({Key key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
  TabController tabController;

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance.init(context);
    Provide.value<IndexProvide>(context).getIndex();
  
   

    return Provide<IndexProvide>(builder: (context, child, index) {
      final _loadingContainer = Container(
          width: ScreenUtil().setHeight(1080),
          height: ScreenUtil().setHeight(1920),
          constraints: BoxConstraints.expand(),
          color: Colors.black,
          child: Center(
            child: Opacity(
              opacity: 0.9,
              child: SpinKitWave(
                color: Colors.white,
                size: 50.0,
              ),
            ),
          ));
      Widget tabBarView() {
        return TabBarView(
          controller: tabController,
          children: Provide.value<IndexProvide>(context)
              .indexModel
              .data
              .channel
              .map((item) {
            return Select(channel: item);
          }).toList(),
        );
      }

      return index == null || index.indexModel == null
          ? _loadingContainer
          : ( (){  
             tabController = TabController(
                  length: index.indexModel.data.channel.length,
                  vsync: this,
                  initialIndex: 0);
              return Scaffold(
                appBar: AppBar(
                  titleSpacing: ScreenUtil.getInstance().setWidth(10),
                  title: Container(
                    alignment: Alignment.center,
                    height: ScreenUtil.getInstance().setHeight(120),
                  decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(ScreenUtil.getInstance().setSp(20)),
                      color: Colors.grey[200],
                    ),
                    child: Container(
                  
                      child: TextField(
                        
                        onSubmitted: (value){
                           Application.router.navigateTo(context, "/list?keyword=${Uri.encodeComponent(widget.keyword)}");
                        },
                        onChanged: (value){
                          setState(() {
                           widget. keyword=value;
                          });
                        },
                        decoration: InputDecoration(
                          contentPadding: EdgeInsets.only(top:ScreenUtil().setHeight(35)),
                           border: InputBorder.none,
                          prefixIcon: Icon(
                            Icons.search,
                            color: Colors.grey[600],
                          ),
                          suffixIcon: Icon(
                            Icons.photo_camera,
                            color: Colors.grey[600],
                          ),
                        ),
                      )
                    ),
                  ),
                  // actions: <Widget>[
                  //   Container(
                  //     padding: EdgeInsets.only(right: ScreenUtil.getInstance().setWidth(15), top: ScreenUtil.getInstance().setHeight(12)),
                  //     child: Center(
                  //       child: Icon(Icons.security),
                  //     ),
                  //   )
                  // ],
                  bottom: TabBar(
                    isScrollable: true,
                    controller: tabController,
                    labelStyle: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: ScreenUtil.getInstance().setSp(60),
                    ),
                    unselectedLabelStyle: TextStyle(
                        fontSize: ScreenUtil.getInstance().setSp(40),
                        fontWeight: FontWeight.w600,
                        color: Color.fromRGBO(0, 0, 0, 0.1)),
                    indicatorColor: Colors.white,
                 tabs:Provide.value<IndexProvide>(context).indexModel.data.channel.map((item)=>Text(item.name)).toList(),
                  ),
                ),
               body:tabBarView(),
                 bottomNavigationBar: BottomBar());
}) ();
 });
  }
}
